package com.bestcem.xm.txtanls.controller.param.chart;

import cn.hutool.core.lang.Validator;
import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.txtanls.enums.ViewpointTypeEnum;
import com.bestcem.xm.txtanls.grpc.service.param.chart.BaseChartParam;
import com.bestcem.xm.txtanls.grpc.service.param.chart.ViewpointEvaluateParam;
import com.bestcem.xm.txtanls.grpc.v1.enums.EvaluationTypeEnum;
import com.bestcem.xm.txtanls.grpc.v1.enums.SortKeyEnum;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;

/**
 * 观点评价请求参数
 *
 * @author jy.zhao
 * @version 2.0
 * @date 2021/5/13 13:39
 **/
@Getter
@Setter
@AllArgsConstructor
public class ViewpointEvaluateQuery extends BaseChartQuery {

    @ApiModelProperty(value = "观点组id")
    @JsonProperty(value = "view_group_id")
    private String viewpointGid;

    @ApiModelProperty(value = "排序规则,PREVIOUS、CURRENT、COMPARE_PREVIOUS。默认COMPARE_PREVIOUS", example = "COMPARE_PREVIOUS")
    @JsonProperty(value = "sort")
    private String sortKey;

    @NotNull(message = "观点类型不能为空")
    @JsonProperty(value = "view_type")
    private String viewpointType;

    @NotNull(message = "评价类型不能为空")
    @JsonProperty(value = "comment_type")
    private String evaluationType;

    public ViewpointEvaluateParam getViewpointEvaluateParam() {
        BaseChartParam baseChartParam = super.getBaseChartParam();
        ViewpointEvaluateParam viewpointEvaluateParam = new ViewpointEvaluateParam();
        BeanUtils.copyProperties(baseChartParam, viewpointEvaluateParam);
        viewpointEvaluateParam.setViewpointGid(this.viewpointGid);
        // 设置评价类型
        if (Validator.isEmpty(this.evaluationType)) {
            viewpointEvaluateParam.setEvaluationType(EvaluationTypeEnum.EvaluationType.GOOD_VALUE);
        } else if (Constants.GOOD.equals(this.evaluationType.toUpperCase())) {
            viewpointEvaluateParam.setEvaluationType(EvaluationTypeEnum.EvaluationType.GOOD_VALUE);
        } else if (Constants.BAD.equals(this.evaluationType.toUpperCase())) {
            viewpointEvaluateParam.setEvaluationType(EvaluationTypeEnum.EvaluationType.BAD_VALUE);
        }
        // 设置观点类型
        if (Validator.isEmpty(this.viewpointType)) {
            viewpointEvaluateParam.setViewpointType(ViewpointTypeEnum.PARENT.getIndex());
        } else if (Constants.VIEW_GROUP.equals(this.viewpointType.toUpperCase())) {
            viewpointEvaluateParam.setViewpointType(ViewpointTypeEnum.PARENT.getIndex());
        } else if (Constants.VIEW.equals(this.viewpointType.toUpperCase())) {
            viewpointEvaluateParam.setViewpointType(ViewpointTypeEnum.CHILDREN.getIndex());
        }
        // 设置排序规则
        if (Validator.isEmpty(this.sortKey)) {
            viewpointEvaluateParam.setSortKey(SortKeyEnum.SortKey.COMPARE_PREVIOUS.name());
        } else {
            viewpointEvaluateParam.setSortKey(this.sortKey.toUpperCase());
        }
        return viewpointEvaluateParam;
    }

    public ViewpointEvaluateQuery(String projectId, String fieldId, String filterType, String current, LocalDate startTime, LocalDate endTime, String viewpointGid, String sortKey, String viewpointType, String evaluationType) {
        super(projectId, fieldId, filterType, current, startTime, endTime);
        this.viewpointGid = viewpointGid;
        this.sortKey = sortKey;
        this.viewpointType = viewpointType;
        this.evaluationType = evaluationType;
    }
}
